#ifndef _DIM_SUM_CAPABILITY_H
#define _DIM_SUM_CAPABILITY_H

#include <dim-sum/types.h>
#include <linux/compiler.h>
#include <uapi/linux/capability.h>

typedef __u32 kernel_cap_t;

/**
 * 忽略对文件和组的拥有者进行改变的限制
 */
#define CAP_CHOWN            0

#define CAP_DAC_OVERRIDE     1

#define CAP_DAC_READ_SEARCH  2
/**
 * 一般是忽略对文件拥有者的权限检查
 */
#define CAP_FOWNER           3
/**
 * 忽略对文件setid和setgid标志设置的限制
 */
#define CAP_FSETID           4

#define CAP_FS_MASK          0x1f

#define CAP_KILL             5

#define CAP_SETGID           6

#define CAP_SETUID           7

#define CAP_SETPCAP          8

#define CAP_LINUX_IMMUTABLE  9

#define CAP_NET_BIND_SERVICE 10

#define CAP_NET_BROADCAST    11

#define CAP_NET_ADMIN        12

#define CAP_NET_RAW          13

#define CAP_IPC_LOCK         14

#define CAP_IPC_OWNER        15

#define CAP_SYS_MODULE       16

#define CAP_SYS_RAWIO        17

#define CAP_SYS_CHROOT       18

#define CAP_SYS_PTRACE       19

#define CAP_SYS_PACCT        20

/**
 * 允许一般的系统管理
 */
#define CAP_SYS_ADMIN        21

#define CAP_SYS_BOOT         22

#define CAP_SYS_NICE         23

#define CAP_SYS_RESOURCE     24

#define CAP_SYS_TIME         25

#define CAP_SYS_TTY_CONFIG   26

#define CAP_MKNOD            27

#define CAP_LEASE            28

#define CAP_AUDIT_WRITE      29

#define CAP_AUDIT_CONTROL    30

#define to_cap_t(x) (x)
#define cap_t(x) (x)

#define CAP_EMPTY_SET       to_cap_t(0)
#define CAP_FULL_SET        to_cap_t(~0)
#define CAP_INIT_EFF_SET    to_cap_t(~0 & ~CAP_TO_MASK(CAP_SETPCAP))
#define CAP_INIT_INH_SET    to_cap_t(0)

#define CAP_TO_MASK(x) (1 << (x))
#define cap_raise(c, flag)   (cap_t(c) |=  CAP_TO_MASK(flag))
#define cap_lower(c, flag)   (cap_t(c) &= ~CAP_TO_MASK(flag))
#define cap_raised(c, flag)  (cap_t(c) & CAP_TO_MASK(flag))

static inline kernel_cap_t cap_combine(kernel_cap_t a, kernel_cap_t b)
{
     kernel_cap_t dest;
     cap_t(dest) = cap_t(a) | cap_t(b);
     return dest;
}

static inline kernel_cap_t cap_intersect(kernel_cap_t a, kernel_cap_t b)
{
     kernel_cap_t dest;
     cap_t(dest) = cap_t(a) & cap_t(b);
     return dest;
}

static inline kernel_cap_t cap_drop(kernel_cap_t a, kernel_cap_t drop)
{
     kernel_cap_t dest;
     cap_t(dest) = cap_t(a) & ~cap_t(drop);
     return dest;
}

static inline kernel_cap_t cap_invert(kernel_cap_t c)
{
     kernel_cap_t dest;
     cap_t(dest) = ~cap_t(c);
     return dest;
}

#define cap_isclear(c)       (!cap_t(c))
#define cap_issubset(a,set)  (!(cap_t(a) & ~cap_t(set)))

#define cap_clear(c)         do { cap_t(c) =  0; } while(0)
#define cap_set_full(c)      do { cap_t(c) = ~0; } while(0)
#define cap_mask(c,mask)     do { cap_t(c) &= cap_t(mask); } while(0)

#define cap_is_fs_cap(c)     (CAP_TO_MASK(c) & CAP_FS_MASK)
#endif /* _DIM_SUM_CAPABILITY_H */
